﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


public partial class AddUpdateAdvertise : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            load_RecordStatus();
            load_Position();

            if (Request.QueryString["id"] != null)
            {
                int iID = int.Parse(Request.QueryString["id"].ToString());
                load_Advertise(iID);
            }
        }
    }

    public void load_Advertise(int iID)
    {
        DataSet dsAdvertise = BusLogic.Advertise.get_ADVERTISE(iID);

        txtName.Text = dsAdvertise.Tables[0].Rows[0]["NAME"].ToString();

        if (!String.IsNullOrEmpty(dsAdvertise.Tables[0].Rows[0]["IMAGE"].ToString()))
        {
            imgNews.Src = dsAdvertise.Tables[0].Rows[0]["IMAGE"].ToString();
            hfImage.Value = dsAdvertise.Tables[0].Rows[0]["IMAGE"].ToString();
        }
        else
        {
            imgNews.Src = "~/App_Themes/Images/toolbar/images.png";
        }
        txtUrl.Text = dsAdvertise.Tables[0].Rows[0]["URL"].ToString();
        ddlPosition.SelectedValue = dsAdvertise.Tables[0].Rows[0]["POSITION"].ToString();
        txtSorted.Text = dsAdvertise.Tables[0].Rows[0]["SORTED"].ToString();
        rdlRecordStatus.SelectedValue = dsAdvertise.Tables[0].Rows[0]["RECORD_STATUS"].ToString();
    }

    protected void lbtSave_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] == null)
        {
            string strImageAdvertise = hfImage.Value.ToString();
            BusLogic.Advertise.add_ADVERTISE(txtName.Text, strImageAdvertise, txtUrl.Text, int.Parse(ddlPosition.SelectedValue.ToString()), int.Parse(txtSorted.Text), Boolean.Parse(rdlRecordStatus.SelectedValue));
        }
        else
        {
            int iID = int.Parse(Request.QueryString["id"].ToString());
            string strImageAdvertise = hfImage.Value.ToString();
            BusLogic.Advertise.update_ADVERTISE(iID, txtName.Text, strImageAdvertise, txtUrl.Text, int.Parse(ddlPosition.SelectedValue.ToString()), int.Parse(txtSorted.Text), Boolean.Parse(rdlRecordStatus.SelectedValue));
        }

        Response.Redirect("AdvertiseManager.aspx");
    }

    public void load_RecordStatus()
    {
        ListItem item = new ListItem();

        item.Value = "True";
        item.Text = "Công bố";
        item.Selected = true;
        rdlRecordStatus.Items.Add(item);

        item = new ListItem();
        item.Value = "False";
        item.Text = "Chờ";
        rdlRecordStatus.Items.Add(item);
    }

    public void load_Position()
    {
        ListItem item = new ListItem();

        item.Value = "1";
        item.Text = "Trung Tâm";
        item.Selected = true;
        ddlPosition.Items.Add(item);

        item = new ListItem();
        item.Value = "2";
        item.Text = "Tin Tức";
        ddlPosition.Items.Add(item);

        item = new ListItem();
        item.Value = "3";
        item.Text = "Đào Tạo";
        ddlPosition.Items.Add(item);

        item = new ListItem();
        item.Value = "4";
        item.Text = "Hình Ảnh";
        ddlPosition.Items.Add(item);
    }
}
